package com.qs.task;

/**
 * ClassName: EsStoreTask
 * Package: com.qs.task
 * Description:
 *
 * @Author: @weixueshi
 * @Create: 2023/12/30 - 22:33
 * @Version: v1.0
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.constant.StatusConstant;
import com.qs.es.service.EsGoodsService;
import com.qs.es.service.EsStoreService;
import com.qs.mapper.GoodsMapper;
import com.qs.mapper.StoreMapper;
import com.qs.pojo.entity.Goods;
import com.qs.pojo.entity.Store;
import com.qs.pojo.es.EsGoodsDto;
import com.qs.pojo.es.EsStoreDto;
import com.qs.service.IStoreService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 每晚凌晨2点将数据库中的商户数据同步到es中
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class EsTask {

    private final StoreMapper storeMapper;

    private final EsStoreService esStoreService;

    private final EsGoodsService esGoodsService;

    private final GoodsMapper goodsMapper;

    /**
     * 将数据库中的所有店铺数据导入到es中
     */
    @Scheduled(cron = "0 1 * * * ?")
    public void saveStoreToEsTask(){
        log.info("店铺数据导入定时任务开始-------------------");
        long begin = System.currentTimeMillis();
        QueryWrapper<Store> wrapper = new QueryWrapper<>();
        wrapper.eq("status", StatusConstant.STATUS_STORE_PASS);
        List<Store> list = storeMapper.selectList(wrapper);
        List<EsStoreDto> esStoreDtos = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            list.stream().forEach(item -> {
                EsStoreDto esStoreDto = new EsStoreDto();
                esStoreDto.setStore(item.getHomeName());
                esStoreDto.setKey(item.getId());
                esStoreDto.setStoreImage(item.getImage());
                esStoreDtos.add(esStoreDto);
            });
            try {
                esStoreService.bulkStoreGoods(esStoreDtos);
                long end = System.currentTimeMillis();
                long time = end - begin;
                log.info("店铺数据导入定时任务完成---------：消耗：{}ms",time);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 将数据库中的所有商品数据导入到es中
     */
    @Scheduled(cron = "0 1 * * * ?")
    public void saveGoodsToEsTask(){
        log.info("商品数据导入定时任务开始-------------------");
        long begin = System.currentTimeMillis();
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("status", StatusConstant.STATUS_GOODS);
        List<Goods> list = goodsMapper.selectList(wrapper);
        List<EsGoodsDto> esGoodsDtos = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            list.stream().forEach(item -> {
                Store store = storeMapper.selectById(item.getStoreId());
                EsGoodsDto esGoodsDto = new EsGoodsDto();
                esGoodsDto.setSale(item.getSale());
                esGoodsDto.setName(item.getName());
                esGoodsDto.setStoreId(store.getId());
                esGoodsDto.setImage(item.getImage());
                esGoodsDto.setPrice(item.getPrice());
                esGoodsDto.setKey(item.getId());
                esGoodsDto.setStore(store.getHomeName());
                esGoodsDto.setStoreImage(store.getImage());
                esGoodsDtos.add(esGoodsDto);
            });
            try {
                esGoodsService.bulkSaveGoods(esGoodsDtos);
                long end = System.currentTimeMillis();
                long time = end - begin;
                log.info("商品数据导入定时任务完成---------：消耗：{}ms",time);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
